package com.ruge.test.Velocity;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.velocity.tools.generic.DateTool;
import org.junit.Test;

import java.io.StringWriter;
import java.util.*;

/**
 * 爱丽丝、如歌  创建于 2018/7/10 20:29
 * 说明:  TODO
 */
public class Velociry_03_Collection {
    @Test
    public void test1(){

        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityEngine.init();

        VelocityContext context = new VelocityContext();
        List<String> strings = Arrays.asList("第一个", "第二个", "第三个", "第四个");
        context.put("strings",strings);
        int[] arr = new int[] {1,2,3,4,5,6};
        context.put("arr",arr);

        List<VelocityModel> velocityModels = Arrays.asList(
                /*new VelocityModel(1, "张1"),
                new VelocityModel(2, "张2"),
                new VelocityModel(3, "张3"),
                new VelocityModel(4, "张4")*/
        );
        context.put("velocityModels",velocityModels);

        Map<String,Object> map = new HashMap<>();
        map.put("strings",strings);
        map.put("arr",arr);
        map.put("velocityModels",velocityModels);
        context.put("map",map);

        context.put("value",true);

        context.put("now",new Date());
        context.put("dateformate",new DateTool());
        String str = "velocity/Velocity_03_collection.vm";
        Template template = velocityEngine.getTemplate(str);

        StringWriter stringWriter = new StringWriter();
        template.merge(context,stringWriter);
        stringWriter.flush();
        System.out.println(stringWriter.toString());
    }
}
